#!/usr/bin/env bash

set -o errexit # Error on any command error
set -o nounset # Fail if variable unbound

GREEN="\033[0;32m"
RED="\033[0;31m"
NO_COLOUR="\033[0m"

ROOT="$(git rev-parse --show-toplevel)"
UCFG="${ROOT}/tools/uncrustify/uncrustify.cfg"
FAILED=()

# Stash all non staged changes
git diff --full-index --binary > /tmp/stash.$$
git stash -q --keep-index

echo "Analysing coding standard ..."

while read -r file ; do

    # Compare staged file with uncrutifyed version
    diff <(uncrustify -q -c "${UCFG}" -f "${file}") <(cat "${file}") > /dev/null && rc=$? || rc=$?
    if [ ${rc} != 0 ] ; then
        echo -e "${RED}[FAIL]${NO_COLOUR} ${file}"
        FAILED+=("${file}")
    else
        echo -e "${GREEN}[PASS]${NO_COLOUR} ${file}"
    fi

done < <(git diff-index --cached --name-only HEAD --diff-filter=ACMR | egrep "\.(c|h|cpp|hpp)$")

# Unstash workspace
# Note: git apply can fail if no unstaged files exists
git apply --whitespace=nowarn < /tmp/stash.$$ 2> /dev/null || true
git stash drop -q
rm /tmp/stash.$$

# Check if all files passed
if [ ${#FAILED[@]} -eq 0 ]; then
    exit 0
fi

# Print files that failed
echo "Please run:"
echo "pushd ${ROOT} && ./tools/uncrustify/uncrustify.sh ${FAILED[*]// /|} && popd"
exit 1